<?php
class soap_chain_with_characteristics
{
	/**
	 * Building
	 *
	 * @var soap_building
	 */

	public $Building = array();
	/**
	 * Alphabet
	 *
	 * @var soap_alphabet_base
	 */
	public $Alphabet = array();
	/**
	 * Dimensions
	 *
	 * @var soap_array_of_dimension
	 */
	public $Dimensions = array();
	/**
	 * Characteristics
	 *
	 * @var soap_array_of_characteristic
	 */
	public $Characteristics = array();
	/**
	 * StartIntervals
	 *
	 * @var soap_frequency_list
	 */
	public $StartIntervals = array();
	/**
	 * CommonIntervals
	 *
	 * @var soap_frequency_list
	 */
	public $CommonIntervals = array();
	/**
	 * EndIntervals
	 *
	 * @var soap_frequency_list
	 */
	public $EndInterval = array();
	/**
	 * Db field name
	 *
	 * 
	 */
	public $Chain_gamut_name = 'ChainAnalises.Classes.IntervalAnalysis.Characteristics.Calculators.Gamut';


	function __construct() {
		$this->Building = new soap_building();
		$this->Characteristics = new soap_array_of_characteristic();
		$this->CommonIntervals = new soap_frequency_list();
		$this->EndInterval = new soap_frequency_list();
		$this->StartIntervals = new soap_frequency_list();
		$this->Dimensions = new soap_array_of_dimension();
	}

	/**
	 * Laod chain from db
	 *
	 * @param int $chain_id
	 */
	public function load_from_db($chain_id) 
	{
		$this->Chain_id = $chain_id;
		$chain = new chain_alphabet_composition();
		$chain->chain->chain_id->set_value($chain_id);

		$dba = db_adapter::get_instance();
		$dba->load_composite($chain);

		$temp = $chain->chain->building->get_value();
		$this->Building = new soap_building();
		$temp = explode('|', $temp);
		$this->Building->long = (count($temp) == 1)? $temp[0] : $temp;
		$this->load_dimenstions($chain_id);
		$this->load_characteristics($chain_id);
		
		$this->load_intervals($chain_id, $this->CommonIntervals ,'both');
		$this->load_intervals($chain_id, $this->EndInterval ,'end');
		$this->load_intervals($chain_id, $this->StartIntervals ,'start');
		
		$this->load_alphabet($chain);				
	}

	/**
	 * laod dimensions
	 *
	 * @param int $chain_id
	 */
	protected function load_dimenstions($chain_id)
	{
		$this->Dimensions = new soap_array_of_dimension();
		$this->Dimensions->load_from_db($chain_id);
	}

	/**
	 * load characteristics
	 *
	 * @param int $chain_id
	 */
	protected function load_characteristics($chain_id)
	{
		$this->Characteristics = new soap_array_of_characteristic();
		$this->Characteristics->load_from_db($chain_id);
	}

	/**
	 * load intervals
	 *
	 * @param int $chain_id
	 * @param string $type
	 */
	protected function load_intervals($chain_id, &$interval_list, $type)
	{
		$interval_list = new soap_frequency_list();
		$interval_list->load_from_db($chain_id, $type);
	}


	/**
	 * Save inteervals list to DB
	 *
	 * @param int $chain_id
	 * @param enum ('common' ,'end' ,'start') $type
	 */
	protected function save_intervals_to_db($chain_id, &$interval_list, $type)
	{
		$interval_list->chain_id = $chain_id;
		$interval_list->type = $type;
		$interval_list->save_to_db($chain_id);
	}
	/**
	 * Load alphabet
	 *
	 * @param soap_chain_with_characteristic $chain
	 */
	protected function load_alphabet($chain)
	{
		$type = $chain->alphabet->alphabet_type->get_value();
		if ($type == 'chain')
		{
			$this->Alphabet = new soap_alphabet_chains();
		}elseif($type == 'char')
		{
			$this->Alphabet = new soap_alphabet_value_char();
		}
		$this->Alphabet->load_from_db($chain->chain->alphabet_id->get_value());
		//var_dump($this->Alphabet);
	}

	public function save_to_db($type)
	{
		$chain = new chain();

		$chain->sequence_id->set_value($this->sequence_id);
		$temp_building = is_array($this->Building->long)? implode("|",$this->Building->long) : $this->Building->long;
		
		$chain->building->set_value($temp_building);
		$chain->chain_md5_building->set_value(md5($temp_building));
		$chain->chain_length->set_value(count($this->Building->long));
/*		$characteristic = $this->Characteristics;
		foreach($characteristic->as_array() as $element){
            if ($element->Type == $this->Chain_gamut_name){
            	$chain->chain_gamut->set_value($element->Start);
            }
		}*/
		
		$chain->chain_type->set_value($type);
		$chain->parent_id->set_value($this->parent_id);
		$chain->experiment_id->set_value($this->experiment_id);
		$chain->alphabet_id->set_value($this->Alphabet->save_to_db());
		
		db_adapter::get_instance()->save_unique($chain);
		
		$parent_id = $chain->chain_id->get_value();
		$this->Dimensions->save_to_db($parent_id);
		$this->Characteristics->save_to_db($parent_id);
		$this->save_intervals_to_db($parent_id, $this->CommonIntervals, 'both');
		$this->save_intervals_to_db($parent_id, $this->EndInterval, 'end');
		$this->save_intervals_to_db($parent_id, $this->StartIntervals, 'start');

		return $parent_id;
	}
}
?>